<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.springframework.security.BadCredentialsException"%>
<%@page import="net.test.util.SpringUtil"%>
<%@page import="net.test.service.AdminService"%>
<%@page import="net.test.entity.Admin"%>
<%@page import="org.springframework.security.DisabledException"%>
<%@page import="org.springframework.security.LockedException"%>
<%@page import="org.springframework.security.AccountExpiredException"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%
	response.setHeader("progma", "no-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Cache-Control", "no-store");
	response.setDateHeader("Expires", 0);

	final String SPRING_SECURITY_LAST_EXCEPTION = "SPRING_SECURITY_LAST_EXCEPTION";

	String base = request.getContextPath();
	ApplicationContext applicationContext = SpringUtil
			.getApplicationContext();
	if (applicationContext == null) {
%>
<%
	return;
	}

	AdminService adminService = (AdminService) SpringUtil
			.getBean("adminServiceImpl");
	String message = null;
	String username = null;

	String error = StringUtils.trim(request.getParameter("error"));
	if (StringUtils.equalsIgnoreCase(error, "captcha")) {
		message = "验证码错误,请重新输入!";
	} else {
		Exception springSecurityLastException = (Exception) session
				.getAttribute(SPRING_SECURITY_LAST_EXCEPTION);
		if (springSecurityLastException != null) {
			if (springSecurityLastException instanceof BadCredentialsException) {
				username = ((String) session
						.getAttribute("SPRING_SECURITY_LAST_USERNAME"))
						.toLowerCase();
				Admin admin = adminService.getAdminByUsername(username);
				if (admin != null) {
					message = "您的用户名或密码错误!";
				} else {
					message = "您的用户名或密码错误!";
				}
			} else if (springSecurityLastException instanceof DisabledException) {
				message = "您的账号已被禁用,无法登录!";
			} else if (springSecurityLastException instanceof LockedException) {
				message = "您的账号已被锁定,无法登录!";
			} else if (springSecurityLastException instanceof AccountExpiredException) {
				message = "您的账号已过期,无法登录!";
			} else {
				message = "出现未知错误,无法登录!";
			}
			session.removeAttribute(SPRING_SECURITY_LAST_EXCEPTION);
		}
	}
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>eportal--登陆</title>
    <meta charset="utf-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
 	<link href="<%=base %>/bootstrap/css/bootstrap.css" rel="stylesheet">
    <style>
    	body {
		  padding-top: 50px;
		}
		.starter-template {
		  padding: 40px 15px;
		  text-align: center;
		}
    </style>
  <script type="text/javascript" src="<%=base %>/template/common/jquery.js"></script>
  <script src="<%=base %>/bootstrap/js/bootstrap.min.js"></script>
  <script type="text/javascript">
    $(function(){
		      // 刷新验证码
			$("#captchaImage").click( function() {
				var timestamp = (new Date()).valueOf();
				var imageSrc = $("#captchaImage").attr("src");
				if(imageSrc.indexOf("?") >= 0) {
					imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
				}
				imageSrc = imageSrc + "?timestamp=" + timestamp;
				$("#captchaImage").attr("src", imageSrc);
			});
       });
  </script>
  </head>
  
  <body>
  <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">eportal</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">主页</a></li>
            <li><a href="#about">关于我们</a></li>
            <li><a href="#contact">其他</a></li>
          </ul>
        </div>
      </div>
    </div>
    <form action="<%=base%>/admin/login/check" method="post">
    <div class="container" style="width:500px">
      <div class="starter-template">
        <h1>eportal</h1>
        <p class="lead">
        	私人定制企业门户网站
        </p>
       	<div class="input-group input-group-lg">
      		<% if(message!=null&&!"null".equals(message)){ %>
	 			<span class="input-group-addon" style="color: red"><%=message%></span>
	 		<%} %>
      	</div><br>
        <div class="input-group input-group-lg">
		  <span class="input-group-addon">用户名</span>
		  <input type="text" id="username" name="j_username" class="form-control" placeholder="请输入用户名">
		</div><br>
		<div class="input-group input-group-lg">
		  <span class="input-group-addon">密&nbsp;码</span>
		  <input type="password" id="password" name="j_password" class="form-control" placeholder="请输入密码">
		</div><br>
		<div class="input-group input-group-lg">
		  <span class="input-group-addon">验证码</span>
		  <input type="text" class="form-control" style="height: 60px" id="captcha" name="j_captcha" placeholder="请输入验证码">
		  <span class="input-group-addon"><img id="captchaImage" src="<%=base%>/captcha.jpeg" alt="换一张" /></span>
		</div>
		<button type="submit" class="btn navbar-btn" style="width:440px; height: 40px">登&nbsp;&nbsp;&nbsp;&nbsp;陆</button>
      </div>
    </div>
   </form>
   
   <div class="container" style="bottom:0;position:absolute">
      <div class="starter-template" style="height:150px">
      	<hr>
      	<p>如有问题，请联系我们<a href="#">satandaice@gmail.com</a></p>
    	<p>京ICP备11008151号<p>
      </div>
   </div>
  </body>
</html>
